package com.milk.system.controller;

import com.milk.system.common.result.PageResult;
import com.milk.system.common.result.Result;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.milk.system.model.form.ProductsForm;
import com.milk.system.model.query.ProductsPageQuery;
import com.milk.system.model.vo.ProductsPageVO;
import com.milk.system.service.ProductsService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import jakarta.validation.Valid;


/**
 * 商品信息表 前端控制器
 *
 * @author milk
 * @since 2024-05-02
 */
@Tag(name = "商品信息表接口")
@RestController
@RequestMapping("/api/business/products")
@RequiredArgsConstructor
public class ProductsController {

    private final ProductsService productsService;

    @Operation(summary = "商品信息表分页列表")
    @GetMapping("/page")
    public PageResult<ProductsPageVO> listPagedProductss(ProductsPageQuery queryParams) {
        IPage<ProductsPageVO> result = productsService.listPagedProductss(queryParams);
        return PageResult.success(result);
    }

    @Operation(summary = "新增商品信息表")
    @PostMapping
    public Result saveProducts(@RequestBody @Valid ProductsForm formData) {
        boolean result = productsService.saveProducts(formData);
        return Result.judge(result);
    }

    @Operation(summary = "商品信息表表单数据")
    @GetMapping("/{id}/form")
    public Result<ProductsForm> getProductsForm(
            @Parameter(description = "商品信息表ID") @PathVariable Long id) {
        ProductsForm formData = productsService.getProductsFormData(id);
        return Result.success(formData);
    }

    @Operation(summary = "修改商品信息表")
    @PutMapping(value = "/{id}")
    public Result updateProducts(@Parameter(description = "商品信息表ID") @PathVariable Long id,
                                 @RequestBody @Validated ProductsForm formData) {
        boolean result = productsService.updateProducts(id, formData);
        return Result.judge(result);
    }

    @Operation(summary = "删除商品信息表")
    @DeleteMapping("/{ids}")
    public Result deleteProductss(
            @Parameter(description = "商品信息表ID，多个以英文逗号(,)分割") @PathVariable String ids) {
        boolean result = productsService.deleteProductss(ids);
        return Result.judge(result);
    }
}
